package com.sbpm.crmbpm.server.mgmt.impl;

import com.sbpm.crmbpm.client.services.CompanyManagement;
import com.sbpm.crmbpm.server.dao.CompanyDao;
import com.sbpm.crmbpm.server.utils.JpaUtils;
import com.sbpm.crmbpm.shared.data.client.Company;
import com.sbpm.crmbpm.shared.data.client.Segment;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.data.shared.loader.PagingLoadResultBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service( "companyManagement" )
@Transactional( propagation = Propagation.REQUIRED )
public class CompanyManagementImpl implements CompanyManagement
{
    private static final Logger log = LoggerFactory.getLogger( CompanyManagementImpl.class );

    @Autowired
    private CompanyDao companyDao;

//    @Autowired
//    private ProcessService processService;

    @Override
    public List<Company> findCompanies( String name, String legalName, String inn, String ogrn, String kpp, Segment segment )
    {
        List< Company > companies = companyDao.findCompanies( name, legalName, inn, ogrn, kpp, segment );
        try
        {
            JpaUtils utils = new JpaUtils();
            return ( List< Company > ) utils.cloneObjectAndDetach( companies );
        }
        catch ( Exception e )
        {
            throw new RuntimeException( e );
        }
    }

    public void saveCompany( Company company )
    {
        if( company.getId() == null || company.getId() == 0 )
        {
            createCompany( company );
        }
        else
        {
            updateCompany( company );
        }
    }

    public void updateCompany( Company company )
    {
        Company savedCompany = companyDao.findById( company.getId() );
        savedCompany.setInn( company.getInn() );
        savedCompany.setKpp( company.getKpp() );
        savedCompany.setType( company.getType() );
        savedCompany.setSegment( company.getSegment() );
        savedCompany.setLegalName( company.getLegalName() );
        savedCompany.setName( company.getName() );
        savedCompany.setOgrn( company.getOgrn() );
    }


    public Long createCompany( Company company )
    {
//        Long id = processService.createInsuranceRequestProcessInstance();
//        log.debug( "id = " + id );
        return companyDao.create( company );
    }

    public PagingLoadResult<Company> findCompanies( String value, int offset, int limit )
    {

        int count = companyDao.findCompaniesCount( value );
        PagingLoadResultBean<Company> result = new PagingLoadResultBean<Company>();
        result.setTotalLength( count );
        result.setOffset( offset );
        if( count > 0 )
        {
            List< Company > companies = companyDao.findCompanies( value, offset, limit );
            try
            {
                JpaUtils utils = new JpaUtils();
                result.setData( ( List< Company > ) utils.cloneObjectAndDetach( companies ) );
            }
            catch ( Exception e )
            {
                throw new RuntimeException( e );
            }
        }
        return result;
    }

    public Company findCompanyWithAllAttributes( Long id )
    {
        Company company = companyDao.findCompanyWithAllAttributes( id );
        try
        {
            JpaUtils utils = new JpaUtils();
            return ( Company ) utils.cloneObjectAndDetach( company );
        }
        catch ( Exception e )
        {
            throw new RuntimeException( e );
        }
    }

    @Override
    public Company findById( Long id )
    {
        try
        {
            JpaUtils utils = new JpaUtils();
            return ( Company ) utils.cloneObjectAndDetach( companyDao.findById( id ) );
        }
        catch ( Exception e )
        {
            throw new RuntimeException( e );
        }
    }


}
